package com.aner.tester.config;

import com.aner.tester.constant.Constant;
import com.aner.tester.exception.ReadYamlFailureException;
import com.aner.tester.utils.text.StringUtils;
import org.slf4j.Logger;
import com.aner.tester.utils.LoggerUtils;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * aner-tester configuration
 *
 * @author aner
 * @version 1.0
 * @date 2020/12/11 14:52
 */
public class AnerTesterConfig {

    private static Logger logger = LoggerUtils.getLogger(AnerTesterConfig.class);

    private static String profilePath= "/application-anertester.yml";

    private static Map yamlMap = new HashMap<>();

    /**
     * parameter object package
     */
    private  String resolverObjectPackage;

    /**
     * controller package
     */
    private String resolverControllerPackage;

    /**
     * parameter object nested level count
     */
    private int nestedLevelCount;

    /**
     * controller name without test
     */
    private String excludeController;

    /**
     * random test date
     */
    private String randomData;

    /**
     * date format
     */
    private String dateFormat;

    /**
     * form data type
     */
    private String contentTypeFormData;

    /**
     * payload data type
     */
    private String contentTypePayload;

    /**
     * token identification
     */
    private String header;

    /**
     *token value
     */
    private String testToken;

    /**
     * whether authorization info is required where the api are be tested
     */
    private boolean login;

    public void init() {

        logger.info("Reading aner-tester configuration file...");
        readYaml();
        Map<String, Object> anerTesterConfigMap = (Map<String, Object>)yamlMap.get("aner-tester");
        resolverObjectPackage = (String)anerTesterConfigMap.get("resolver-object-package");
        resolverControllerPackage = (String) anerTesterConfigMap.get("resolver-controller-package");
        nestedLevelCount = (int) anerTesterConfigMap.get("nested-level-count");
        excludeController = (String) anerTesterConfigMap.get("exclude-controller");
        randomData = (String) anerTesterConfigMap.get("random-value");
        dateFormat = (String) anerTesterConfigMap.get("date-format");

        Map<String, String> contentTypeMap = (Map<String, String>)anerTesterConfigMap.get("content-type");
        contentTypeFormData = contentTypeMap.get("form-data");
        contentTypePayload = contentTypeMap.get("payload");

        Map<String, Object> loginInfoMap = (Map<String, Object>)anerTesterConfigMap.get("login-info");
        header = (String)loginInfoMap.get("header");
        testToken = (String)loginInfoMap.get("test-token");
        login = (boolean)loginInfoMap.get("is-login") ;
    }

    private void readYaml() {
        try{
            Yaml yaml=new Yaml();
            InputStream stream = AnerTesterConfig.class.getResourceAsStream(profilePath);
            yamlMap = (HashMap<String,Map>) yaml.load(stream);
        }catch (Exception e){
            throw new ReadYamlFailureException("read yaml file failure");
        }
    }

    public int getNestedLevelCount() {
        return nestedLevelCount;
    }

    public void setNestedLevelCount(int nestedLevelCount) {
        this.nestedLevelCount = nestedLevelCount;
    }

    public String getResolverObjectPackage() {
        return resolverObjectPackage;
    }

    public void setResolverObjectPackage(String resolverObjectPackage) {
        this.resolverObjectPackage = resolverObjectPackage;
    }

    public String getResolverControllerPackage() {
        return resolverControllerPackage;
    }

    public void setResolverControllerPackage(String resolverControllerPackage) {
        this.resolverControllerPackage = resolverControllerPackage;
    }

    public String getDateFormat() {
        if (StringUtils.isNotEmpty(dateFormat)) {
            return dateFormat;
        } else {
            return Constant.DATA_FORMAT;
        }
    }

    public String getExcludeController() {
        return excludeController;
    }

    public void setExcludeController(String excludeController) {
        this.excludeController = excludeController;
    }

    public String getRandomData() {
        return randomData;
    }

    public void setRandomData(String randomData) {
        this.randomData = randomData;
    }

    public void setDateFormat(String dateFormat) {
        this.dateFormat = dateFormat;
    }

    public String getContentTypeFormData() {
        return contentTypeFormData;
    }

    public void setContentTypeFormData(String contentTypeFormData) {
        this.contentTypeFormData = contentTypeFormData;
    }

    public String getContentTypePayload() {
        return contentTypePayload;
    }

    public void setContentTypePayload(String contentTypePayload) {
        this.contentTypePayload = contentTypePayload;
    }

    public String getHeader() {
        return header;
    }

    public void setHeader(String header) {
        header = header;
    }

    public String getTestToken() {
        return testToken;
    }

    public void setTestToken(String testToken) {
        testToken = testToken;
    }

    public boolean isLogin() {
        return login;
    }

    public void setLogin(boolean isLogin) {
        isLogin = isLogin;
    }

    private AnerTesterConfig() {}

    public static AnerTesterConfig me() {
        return AnerTesterConfigHolder.INSTANCE;
    }

    private static class AnerTesterConfigHolder {
        public static final AnerTesterConfig INSTANCE = new AnerTesterConfig();
    }

}
